package org.qydjk.console.service.impl;

import org.qydjk.common.pojo.StoreCard;
import org.qydjk.common.pojo.StoreCardCategory;
import org.qydjk.common.service.impl.BaseService;
import org.qydjk.console.service.IStoreCardService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StoreCardService extends BaseService implements IStoreCardService {


    public int delcardClass(Integer id) throws Exception {
        StoreCardCategory category= baseDao.get(StoreCardCategory.class,id);
        int count = baseDao.getRecordCountSQL("select count(*) from qy_store_card where type<> 0 or receive_seller_id is not null");
        if(count>0){
            return 2;
        }else{
            baseDao.delete(category);
            baseDao.excuteSQL("delete from qy_store_card where store_card_category_id = "+id);
            return 1;
        }
    }

    public int addCard(StoreCard storeCard) throws Exception {
        int store_card_category_id = storeCard.getStoreCardCategoryId();
            StoreCardCategory categoryList = baseDao .get(StoreCardCategory.class,store_card_category_id);
            int codenum = baseDao.getRecordCountSQL("select count(*) from qy_store_card where code = '"+storeCard.getCode()+"'"); if(codenum!=0){
                return 2;
            }
            storeCard.setFaceVal(categoryList.getFaceVal());
            storeCard.setType(0);
            storeCard.setStatus(1);
            storeCard.setCreateTime(new Date());//创建时间
            storeCard.setUpdateTime(new Date()); //修改时间
            baseDao.save(storeCard);
            categoryList.setTotalRepertory(categoryList.getTotalRepertory()+1);       //总库存+1
            categoryList.setRemainRepertory(categoryList.getRemainRepertory()+1); //剩余数量+1
            categoryList.setUpdateTime(new Date());
            baseDao.saveOrUpdate(categoryList);
            return 1;
    }

    public void addCardfromExcel(StoreCardCategory storeCardCategory, List<Map> cardinfo) throws Exception {
        //首先添加储值卡
        for(Map<String,Object> map:cardinfo){
            String sql = "insert into qy_store_card \n" +
                    "(store_card_category_id,code,pwd,face_val,type,create_time,update_time)\n" +
                    "values("+storeCardCategory.getId()+",'"+map.get("col_0")+"','"+map.get("col_1")+"',"+storeCardCategory.getFaceVal()+",0, date_format(now(),'%Y-%m-%d'),date_format(now(),'%Y-%m-%d'))";
            baseDao.excuteSQL(sql);
        }
        //修改卡种类库存
        int count = cardinfo.size();
        storeCardCategory.setTotalRepertory(storeCardCategory.getTotalRepertory()+count);
        storeCardCategory.setRemainRepertory(storeCardCategory.getRemainRepertory()+count);
        baseDao.saveOrUpdate(storeCardCategory);
    }

    public void delCard(Integer id) throws Exception {
        StoreCard  storeCard = baseDao.get(StoreCard.class,id);
        baseDao.delete(storeCard);
        StoreCardCategory category = baseDao.get(StoreCardCategory.class,storeCard.getStoreCardCategoryId());
        category.setTotalRepertory(category.getTotalRepertory()-1);     //卡种类库存-1
        category.setRemainRepertory(category.getRemainRepertory()-1);//卡种类剩余库存-1
        baseDao.saveOrUpdate(category);
    }

    public void updateCard(StoreCard newCard) throws Exception {
        StoreCard oldcard = baseDao.get(StoreCard.class,newCard.getId());
        StoreCardCategory category2 = baseDao.get(StoreCardCategory.class,newCard.getStoreCardCategoryId());        //修改后的卡分类
        //如果修改分类则修改两个库存,只能修改未售的卡，所以只需要该总库存和剩余库存
        if(oldcard.getStoreCardCategoryId()!=newCard.getStoreCardCategoryId()){
            StoreCardCategory category1 = baseDao.get(StoreCardCategory.class,oldcard.getStoreCardCategoryId());      //修改前的卡分类
            category1.setTotalRepertory(category1.getTotalRepertory()-1);
            category1.setRemainRepertory(category1.getRemainRepertory()-1);
            baseDao.update(category1);
            category2 = baseDao.get(StoreCardCategory.class,newCard.getStoreCardCategoryId());
            category2.setTotalRepertory(category2.getTotalRepertory()+1);
            category2.setRemainRepertory(category2.getRemainRepertory()+1);
            baseDao.update(category2);
        }
        oldcard.setCode(newCard.getCode());                                  //卡号
        oldcard.setPwd(newCard.getPwd());                                     //密码
        oldcard.setStoreCardCategoryId(newCard.getStoreCardCategoryId());  //新的分类
        oldcard.setFaceVal(category2.getFaceVal());                                 //面值
        baseDao.saveOrUpdate(oldcard);
    }
}
